package com.warmlight.voicepacket.plugin;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;

import com.facebook.drawee.view.SimpleDraweeView;
import com.warmlight.voicepacket.MApplication;
import com.warmlight.voicepacket.R;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by weiweijinpeng on 2018/7/25.
 * 自定义播放器控件继承VideoPlayer
 */

public class QmzbMediaView extends QmzbVideoPlayer {

    protected static Timer DISMISS_CONTROL_VIEW_TIMER;

    protected DismissControlViewTimerTask mDismissControlViewTimerTask;

    public ImageView mBackground;

    public SimpleDraweeView simpleDraweeView;

    ProgressBar bottomProgressBar;

    boolean flag=true;

    boolean videoFlag=true;

    private Context mContext;

    boolean isclick=true;

    /**
     * 视频缓存地址
     */
    private String mVideoPath = "";         //当前的视频地址

    private String mVideoNextPath = "";     //下一个视频地址

    private int videoPosition;              //记录下位置

    public QmzbMediaView(Context context) {
        super(context);
        this.mContext=context;
    }

    public QmzbMediaView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext=context;
    }

    @Override
    public void init(Context context) {
        super.init(context);
        mBackground = findViewById(R.id.background_view);
        simpleDraweeView = findViewById(R.id.background_draweeview);
        bottomProgressBar = findViewById(R.id.bottom_progress);

    }

    @Override
    public int getLayoutId() {
        return R.layout.item_qmzb_mediaview;
    }

    @Override
    public void onStateNormal() {
        super.onStateNormal();
        changeUiToNormal();
    }

    @Override
    public void onStatePreparing() {
        super.onStatePreparing();
        mPlayerView.setVisibility(View.VISIBLE);
//        changeUiToPreparing();
    }

    @Override
    public void onStatePreparingChangingUrl(int urlMapIndex, long seekToInAdvance) {
        super.onStatePreparingChangingUrl(urlMapIndex, seekToInAdvance);
    }

    @Override
    public void onStatePlaying() {
        super.onStatePlaying();
        changeUiToPlayingClear();
        mPlayerView.setImageResource(R.drawable.ic_video_pause);
    }

    @Override
    public void onStatePause() {
        super.onStatePause();
        changeUiToPauseShow();
    }

    @Override
    public void onStateError() {
        super.onStateError();
        changeUiToError();
    }

    @Override
    public void onStateAutoComplete() {
        super.onStateAutoComplete();
        mPlayerView.setImageResource(R.drawable.comm_video_reset);
        changeUiToComplete();
    }


    @Override
    public void setProgressAndText(int progress, long position, long duration) {
        super.setProgressAndText(progress, position, duration);
        if (progress != 0) bottomProgressBar.setProgress(progress);
    }

    @Override
    public void setBufferProgress(int bufferProgress) {
        super.setBufferProgress(bufferProgress);
        if (bufferProgress != 0) bottomProgressBar.setSecondaryProgress(bufferProgress);
    }

    @Override
    public void resetProgressAndTime() {
        super.resetProgressAndTime();
        bottomProgressBar.setProgress(0);
        bottomProgressBar.setSecondaryProgress(0);
    }

    public void setControlHidden(boolean hidden){
        isShowlayout_bottom = hidden;
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int id = v.getId();
        if (id == R.id.surface_container) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
//                    if (isclick){
                        startDismissControlViewTimer();

//                        isclick=false;
//                    }else if (! isclick){
//                        bottomContainer.setVisibility(View.GONE);
//                        mPlayerView.setVisibility(View.GONE);
//                        isclick=true;
//                    }
                    break;
            }
        } else if (id == R.id.bottom_seek_progress) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_UP:
                    startDismissControlViewTimer();
                    break;
            }
        }
        return super.onTouch(v, event);
    }


    public void changeUiToNormal() {
        switch (currentScreen) {
            case SCREEN_WINDOW_NORMAL:
            case SCREEN_WINDOW_LIST:
                setAllControlsVisiblity(View.VISIBLE);
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisiblity(View.VISIBLE);
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }
    }

    public void changeUiToPreparing() {
        switch (currentScreen) {
            case SCREEN_WINDOW_NORMAL:
            case SCREEN_WINDOW_LIST:
                setAllControlsVisiblity(View.INVISIBLE);
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisiblity(View.INVISIBLE);
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }
    }

    public void changeUiToPlayingClear() {
        switch (currentScreen) {
            case SCREEN_WINDOW_NORMAL:
            case SCREEN_WINDOW_LIST:
                setAllControlsVisiblity(View.INVISIBLE);
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisiblity(View.INVISIBLE);
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }
    }

    public void changeUiToPauseShow() {
        switch (currentScreen) {
            case SCREEN_WINDOW_NORMAL:
            case SCREEN_WINDOW_LIST:
                setAllControlsVisiblity(View.VISIBLE);
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisiblity(View.VISIBLE);
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }
    }
    public void changeUiToComplete() {
        switch (currentScreen) {
            case SCREEN_WINDOW_NORMAL:
            case SCREEN_WINDOW_LIST:
                setAllControlsVisiblity(View.VISIBLE);
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisiblity(View.VISIBLE);
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

    public void changeUiToError() {
        switch (currentScreen) {
            case SCREEN_WINDOW_NORMAL:
            case SCREEN_WINDOW_LIST:
                setAllControlsVisiblity(View.VISIBLE);
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisiblity( View.VISIBLE);
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

    public void setAllControlsVisiblity(int thumbImg) {
        mBackground.setVisibility(thumbImg);
        mPlayerView.setVisibility(thumbImg);
    }

    private OnComPletion onComPletion;

    public interface OnComPletion{
        void onCompletion();
    }

    public void setOnComPletion(OnComPletion onComPletion){
        this.onComPletion = onComPletion;
    }


    @Override
    public void onAutoCompletion() {
        super.onAutoCompletion();
        if (onComPletion != null){
            onComPletion.onCompletion();
        }
    }

    @Override
    public void onCompletion() {
        super.onCompletion();
    }


    public void startDismissControlViewTimer() {
        cancelDismissControlViewTimer();
        DISMISS_CONTROL_VIEW_TIMER = new Timer();
        mDismissControlViewTimerTask = new DismissControlViewTimerTask();
        DISMISS_CONTROL_VIEW_TIMER.schedule(mDismissControlViewTimerTask, 2000);
    }

    public void cancelDismissControlViewTimer() {
        if (DISMISS_CONTROL_VIEW_TIMER != null) {
            DISMISS_CONTROL_VIEW_TIMER.cancel();
        }
        if (mDismissControlViewTimerTask != null) {
            mDismissControlViewTimerTask.cancel();
        }

    }

    public void dissmissControlView() {
        if (currentState != CURRENT_STATE_NORMAL
                && currentState != CURRENT_STATE_ERROR
                && currentState != CURRENT_STATE_AUTO_COMPLETE) {
            post(new Runnable() {
                @Override
                public void run() {
                    bottomContainer.setVisibility(View.INVISIBLE);
                    mPlayerView.setVisibility(View.INVISIBLE);
                }
            });
        }
    }


    public class DismissControlViewTimerTask extends TimerTask {

        @Override
        public void run() {
            dissmissControlView();
        }
    }


    public void setUp(Object[] dataSourceObjects, int defaultUrlMapIndex, int screen, Object... objects) {
        super.setUp(dataSourceObjects, defaultUrlMapIndex, screen, objects);
//        if (objects.length != 0) titleTextView.setText(objects[0].toString());
        if (currentScreen == SCREEN_WINDOW_FULLSCREEN) {
            fullscreen.setImageResource(R.drawable.jz_shrink);
        } else if (currentScreen == SCREEN_WINDOW_NORMAL
                || currentScreen == SCREEN_WINDOW_LIST) {
            fullscreen.setImageResource(R.drawable.jz_enlarge);
        } else if (currentScreen == SCREEN_WINDOW_TINY) {
            setAllControlsVisiblity(View.INVISIBLE);
        }

        if (tmp_test_back) {
            tmp_test_back = false;
            QmzbVideoPlayerManager.setFirstFloor(this);
            backPress();
        }
    }



    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        super.onStartTrackingTouch(seekBar);
        cancelDismissControlViewTimer();

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        super.onStopTrackingTouch(seekBar);
        if (currentState == CURRENT_STATE_PLAYING) {
            dissmissControlView();
        } else {
            startDismissControlViewTimer();
        }
    }

    /**
     *
     */
    public void prepareVideo(String videoUrl){
        mVideoPath = MApplication.getProxy(getContext()).getProxyUrl(videoUrl);
        setVideoData(mVideoPath, QmzbVideoPlayer.SCREEN_WINDOW_NORMAL, "");
        startVideo();
    }
}
